var storagehelper={};
storagehelper.seachhistoryadd = function (key, data, len,keyfield){ 
   if (!len){
    len=6;
  }
  var seachdata = this.seachhistoryget(key,true)  
  if (!seachdata[key]){
    seachdata[key]=[];
  } 
  else{
    var delindex=[]; 
    seachdata[key].forEach(function(nmode,ninex){
      if (nmode && typeof(nmode) =="object")
      {
        if (!keyfield){
          keyfield="name";
        }
        if (nmode[keyfield] == data[keyfield]) {
          delindex.push(ninex);
        }
      }
      else
      {
        if (nmode == data) {
          delindex.push(ninex);
        }
      }         
    }) 
    var dellen = delindex.length-1;
    for (; dellen >=0; dellen--){
      seachdata[key].splice(delindex[dellen],1);
    }    
  }
  if (seachdata[key].length>=len){
    seachdata[key].splice((len - 1), 1)    
  }
  seachdata[key].splice(0, 0, data);
  wx.setStorage({
    key: 'seach',
    data: JSON.stringify(seachdata),
  })

  return seachdata[key];

} 
//清除某个键的值
storagehelper.seachhistoryget = function (key, haskey){
  var seachdata = wx.getStorageSync('seach')
  seachdata = (!seachdata) ? {} : JSON.parse(seachdata); 
  if (haskey){
    return seachdata;
  }
  return seachdata[key] ? seachdata[key] : []  ;
}

//清除某个键的缓存
storagehelper.seachhistorydel = function (key) {
  var seachdata= storagehelper.seachhistoryget(key,true)
  seachdata[key]=[];
  wx.setStorage({
    key: 'seach',
    data: JSON.stringify(seachdata),
  })
}
//对像储存 id 之类 字段猎取
storagehelper.getstrobjlastdata=function(sourcedata, keyfield){
  if (!sourcedata) {
    return {};
  }
  if (!keyfield) {
    keyfield = "id";
  }
  var historylist = storagehelper.seachhistoryget("contact")
  var lastlist = {};
  historylist.forEach(function (nmodel, item) {
    if (nmodel) {
      lastlist["num_" + nmodel] = "-1";
    }
  })
  var temkeyval = "";
  sourcedata.forEach(function (nmodelx, itemx) {
    temkeyval = "num_" + nmodelx[keyfield];
    if (lastlist[temkeyval]) {
      lastlist[temkeyval] = nmodelx;
    }
  })
  return lastlist;
}


storagehelper.setvaliditydata=function(key,data,times){
  var resutdata={};
  if(!times)
  {
    times=(new Date().getTime())/1000+60*60*24;   
  }
  resutdata["times"]=times; 
  resutdata["data"]=data;
  wx.setStorage({
    key: key,
    data: resutdata,
  })
}

storagehelper.getvaliditydata=function(key){
  var data = wx.getStorageSync(key)
    if(data){
      newtimes=(new Date().getTime())/1000+60*60*24;  
      if(newtimes<data["times"]) {
        return {status:-1};
      }
      else
      {
        return {status:-1,data:data};
      }     
    }
    else
    {
      return {status:-1};
    }
}
/**
 * 
 */
storagehelper.setvaliditydata=function(key,data,times){
  var resutdata={};
  if(!times)
  {
    times=(new Date().getTime())/1000+60*60*24;   
  }
  else
  {
    times=(new Date().getTime())/1000 +times*60;  
  }
  resutdata["times"]=times; 
  resutdata["data"]=data;
  wx.setStorage({
    key: key,
    data: resutdata,
  })
}



module.exports = storagehelper